package com.zc.serveudpdemo.tcp;


import android.util.Log;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TcpHelper {

    public static final String TCP_IP_GROUND = "127.0.0.1";
    public static final int TCP_PORT = 5600;

    private static TcpHelper instance = null;
    private Socket socket;
    private DataOutputStream os;
    private DataInputStream inputStream;
    private boolean NeedStop;
    private InetSocketAddress mInetSocketAddress;
    public String ErrMsg;
    /**
     * 读取数据线程
     */
    private ReadThread readThread;
    private long LastCheckTime;
//    private ExecutorService executorService;

    /**
     * 获取当前对象的唯一方法
     */
//    public static TcpHelper getInstance() {
//        if (null == instance) {
//            synchronized (TcpHelper.class) {
//                if (null == instance) {
//                    instance = new TcpHelper();
//                }
//            }
//        }
//        return instance;
//    }

    public TcpHelper(String ip) {
        try {
            LastCheckTime = System.currentTimeMillis();
            readThread = new ReadThread();
            NeedStop = false;
            if (ip == null) {
                mInetSocketAddress = new InetSocketAddress(TCP_IP_GROUND, TCP_PORT);
            } else {
                mInetSocketAddress = new InetSocketAddress(ip, TCP_PORT);
            }
            socket = new Socket();
            readThread.start();
//            executorService = Executors.newScheduledThreadPool(5);
        } catch (Exception e) {
            ErrMsg = e.getMessage();
        }
    }

    public void sendBytes(byte[] iBuf) {
        try {
            if (os != null) {
                os.write(iBuf);
                os.flush();
//                Log.i("sendBytes= ",""+iBuf[0]);
            }
//            FileUtil.method3("/mnt/sdcard/test999.264", iBuf);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isConnected() {
        return socket.isConnected();
    }

    /**
     * 读取线程
     */
    private class ReadThread extends Thread {
        @Override
        public void run() {
            while (!NeedStop) {
                try {
                    if (!socket.isConnected() || socket.isClosed()) {
                        socket.connect(mInetSocketAddress, 3000);
                        if (inputStream == null) {
                            inputStream = new DataInputStream(socket.getInputStream());
                        }
                        if (os == null) {
                            os = new DataOutputStream(socket.getOutputStream());
                        }
                    } else {
                        if (inputStream.available() > 0) {
                            byte[] recData = new byte[inputStream.available()];
                            inputStream.read(recData);
                        }
                        if (System.currentTimeMillis() - LastCheckTime > 5000) {
                            try {
                                LastCheckTime = System.currentTimeMillis();
                                socket.sendUrgentData(0xFF);
                            } catch (Exception e) {
                                socket.close();
                                socket = new Socket();
                                inputStream = null;
                                os = null;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public void stop() {
        if (socket != null) {
            try {
                socket.close();
                socket = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                    inputStream = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                    os = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            NeedStop = true;
//            executorService.shutdown();
        }
    }
}